<?php

namespace backend\controllers;
use Yii;
use backend\controllers\CommontController;
use backend\models\right\JobRight;
use backend\models\admin\JobAdmin;
use backend\models\right\JobAdminRelation;

//继承commont控制器 做防非法登录
class AdminController extends CommontController
{	

    //管理员列表
    public function actionList()
    {   
        $admin_list = JobAdmin::find()->All();
        foreach ($admin_list as $key => $value) 
        {
            $admin_relation = JobAdminRelation::find()->where(['admin_id'=>$value->id])->One();
            //print_r($admin_relation);exit;
        }
        return $this->render('list',['list'=>$admin_list]);      
    }
    //管理员添加
    public function actionAdd()
    {   
        $db = new JobAdmin();
        if (Yii::$app->request->isPost) 
        {   
            $data = Yii::$app->request->post();
            if ($data['password']!=$data['renewpass']) {
               echo "<script>alert('两次密码不一致');
                location.href='?r=right/add'</script>";
            }
            $list = JobAdmin::find()->where(['admin_name'=>$data['admin_name']])->One();
            if ($list) {
                echo "<script>alert('名称已存在');
                location.href='?r=admin/add'</script>";
                exit;
            }
            $db->admin_name = $data['admin_name'];
            $db->password = md5($data['password']);
            $db->create_time = date("Y-m-d H:i:s",time());
            if ($db->insert()) 
            {
                return $this->redirect(['list']);
            }
        }else{
        return $this->render('add');
        }
    }


}
